package top.huhuiyu.springboot3.controller.message;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.huhuiyu.springboot3.annotation.Limit;
import top.huhuiyu.springboot3.base.PageBean;
import top.huhuiyu.springboot3.base.R;
import top.huhuiyu.springboot3.entity.message.TbMessage;
import top.huhuiyu.springboot3.service.message.TbMessageService;
import top.huhuiyu.springboot3.validate.CommonValidate;
import top.huhuiyu.springboot3.vo.PageVO;

@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
public class TbMessageController {
    private final TbMessageService tbMessageService;

    @Limit(key = "/message/insert", message = "不要频繁发帖")
    @PostMapping("")
    public R<String> insert(@RequestBody @Validated(CommonValidate.Add.class) TbMessage tbMessage) {
        return tbMessageService.insert(tbMessage);
    }

    @GetMapping("/{ps}/{pn}")
    public R<PageVO<TbMessage>> query(TbMessage tbMessage, @PathVariable("ps") Long ps, @PathVariable("pn") Long pn) {
        return tbMessageService.query(tbMessage, PageBean.instance(pn, ps));
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Integer id) {
        return tbMessageService.delete(id);
    }

    @PutMapping("")
    public R<String> update(@RequestBody @Validated(CommonValidate.Edit.class) TbMessage tbMessage) {
        return tbMessageService.update(tbMessage);
    }

    @GetMapping("/{id}")
    public R<TbMessage> queryById(@PathVariable("id") Integer id) {
        return tbMessageService.queryById(id);
    }

}
